package cn.zn.modules.pz.cur.controller;

import cn.zn.framework.common.utils.R;
import cn.zn.framework.common.utils.Result;
import cn.zn.modules.pz.cur.entity.TpSpending;
import cn.zn.modules.pz.cur.entity.TpbTripTraffic;
import cn.zn.modules.pz.cur.service.TpSpendingService;
import cn.zn.modules.pz.cur.service.TpbTripTrafficService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("pz/cur/pztraffic")
public class TpTrafficController {

    @Autowired
    private TpbTripTrafficService tpbTripTrafficService;
    @Autowired
    private TpSpendingService tpSpendingService;

    @GetMapping("list/{mainId}")
    public Result list(@PathVariable String mainId) {
        return Result.ok(tpbTripTrafficService.findByMainId(mainId));
    }

    @GetMapping("{id}")
    public R info(@PathVariable("id") String id) {
        return R.ok().put("trip", tpbTripTrafficService.getById(id));
    }

    @PostMapping("save")
    public R save(@Valid @RequestBody TpbTripTraffic tpbTripTraffic) {
        TpSpending tpSpending = tpSpendingService.findByMainId(tpbTripTraffic.getMainId());
        tpbTripTraffic.setTpSpendingId(tpSpending.getId());
        tpbTripTrafficService.save(tpbTripTraffic);
        return R.ok();
    }

    @PostMapping("update")
    public R update(@Valid @RequestBody TpbTripTraffic tpbTripTraffic) {
        tpbTripTrafficService.updateById(tpbTripTraffic);
        return R.ok();
    }

    @DeleteMapping
    public R delete(@RequestBody String[] ids) {
        tpbTripTrafficService.removeByIds(Lists.newArrayList(ids));
        return R.ok();
    }
}
